package com.dhcc.DRGsHosp.application.impl;//loadNotInDiagnosis

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.MrQualityAnalysisApplication;
import com.dhcc.DRGsHosp.core.domain.IndexCal;
import com.dhcc.DRGsHosp.infra.util.StringUtils;

@Named
@Transactional
public class MrQualityAnalysisApplicationImpl implements MrQualityAnalysisApplication {
	@Autowired  
	private NamedParameterJdbcTemplate jdbcTempSeverice;
	@Autowired
	private JdbcTemplateObj jdbcTemplateObj;
	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
	
	/**  
	 *  @Description：获取已分组的病例质量数据
	 *  @Author mazhaoyan  
	 *  @see com.dhcc.DRGsHosp.application.MrQualityAnalysisApplication#getGroupCaseQuality(java.lang.String, java.lang.String)  
	 */
	@Override
	public List<Map<String, Object>> getGroupCaseQuality(IndexCal index) {
		String dateFrom = index.getDateFrom();
		String dateTo = index.getDateTo();
		String stdId = index.getStdId();
		String verRegion = index.getVerRegion();
		String hospCode = index.getHospCode();
		StringBuilder sql = new StringBuilder();
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("dateFrom",dateFrom);
		paramMap.put("dateTo",dateTo);
		paramMap.put("stdId",stdId);
		paramMap.put("verRegion",verRegion);
		paramMap.put("hospCode",hospCode);
		sql.append(" SELECT PD.PARDRG_CODE,DRG.DRG_DESC,COUNT(1) CASECNT,ROUND(AVG(COSTAMOUNT),2) AVGFEE,ROUND(AVG(IN_DAYS),2) AVG_DAY,ROUND(DECODE(AVG(COSTAMOUNT),0,0,STDDEV(COSTAMOUNT)/AVG(COSTAMOUNT)),2) CVFEE,  \n ");
		sql.append(" ROUND(DECODE(AVG(IN_DAYS),0,0, STDDEV(IN_DAYS)/AVG(IN_DAYS)),2) CVDAY,SUM(CASE WHEN ABS(COSTAMOUNT-STD.AS_AVG_FEE)> 0.5*STD.AS_AVG_FEE THEN 1 ELSE 0 END) HIGHDISPCASECNT,  \n ");
		sql.append(" SUM(CASE WHEN ABS(COSTAMOUNT-STD.AS_AVG_FEE) BETWEEN 0.2*STD.AS_AVG_FEE AND 0.5*STD.AS_AVG_FEE THEN 1 ELSE 0 END) COMMONDISPCASECNT,  \n ");
		sql.append(" AVG(STD.AS_AVG_FEE) AS_AVG_FEE,AVG(STD.AS_AVG_DAYS) AS_AVG_DAYS,AVG(STD.AS_WEIGHT) AS_WEIGHT   \n ");
		sql.append(" FROM (SELECT IP.PMAININDEX,IP.P00800000 COSTAMOUNT,IP.P00320000 IN_DAYS FROM DRGS.MR_IPINFO IP LEFT JOIN DRGS.MR_BASEINFO B ON IP.PMAININDEX = B.PMAININDEX WHERE 1 = 1 \n ");
		if(hospCode.contains(",")){
			sql.append(" AND INSTR(:hospCode,B.P00010001)>0 \n ");
		}else{
			sql.append(" AND B.P00010001=:hospCode \n ");
		}
		sql.append(" AND TO_CHAR(IP.P00290000,'YYYY-MM-DD') BETWEEN :dateFrom AND :dateTo ) IP LEFT JOIN DRGS.T_PATIENTDRG PD ON PD.PARID = IP.PMAININDEX  and pd.ver_region=:verRegion \n ");
		sql.append(" LEFT JOIN DRGS.T_ANNUALSTD STD ON STD.AS_DRG_CODE = PD.PARDRG_CODE AND STD.AS_ID = :stdId  \n ");
		sql.append(" LEFT JOIN DRGS.T_DRGS DRG ON DRG.DRG_CODE = PD.PARDRG_CODE AND DRG.VER_CODE = PD.VER_CODE  \n ");
		sql.append(" WHERE PD.PARDRG_CODE IS NOT NULL \n ");
		sql.append(" GROUP BY PD.PARDRG_CODE,DRG.DRG_DESC \n ");
		sql.append(" ORDER BY CVFEE DESC \n ");
		return jdbcTempSeverice.queryForList(sql.toString(),paramMap);
	}

	/**  
	 *  @Description：病组的详细数据
	 *  @Author mazhaoyan  
	 *  @see com.dhcc.DRGsHosp.application.MrQualityAnalysisApplication#getGroupDetail(java.lang.String, java.lang.String, java.lang.String, java.lang.String)  
	 */
	@Override
	public List<Map<String, Object>> getGroupDetail(IndexCal index) {
		StringBuilder sql = new StringBuilder();
		Map<String,Object> paramMap = new HashMap<String,Object>();
		String dateFrom = index.getDateFrom();
		String dateTo = index.getDateTo();
		String hospCode=index.getHospCode();//院区编码
		String code = index.getType1();//代码
		String drgCode = index.getPardrgCode();//drg代码
		String stdId = index.getStdId();
		String verRegion = index.getVerRegion();
		
		paramMap.put("dateFrom",dateFrom);
		paramMap.put("dateTo",dateTo);
		paramMap.put("drgCode",drgCode);
		paramMap.put("stdId",stdId);
		paramMap.put("verRegion",verRegion);
		paramMap.put("hospCode",hospCode);
		
		if("G".equals(code)){
			sql.append(" SELECT NVL(CASE_FLAG,0) CASE_FLAG ,COUNT(*) CASECNT,ROUND(AVG(COSTAMOUNT),2) AVGFEE,ROUND(AVG(IN_DAYS),2) AVGDAY,ROUND(SUM(COSTAMOUNT)/SUM(IN_DAYS),2) DAYFEE FROM( \n ");
		}else if(code.matches("[0-5]")){
			sql.append(" SELECT OUT_DEP_CODE,DEP_NAME, COUNT(*) CNT FROM( \n ");
		}
		sql.append(" SELECT IP.PMAININDEX,IP.MR_NO,IP.PNAME,IP.AGE_YEAR,IP.AGE_MONTH,IP.AGE_DAY,IP.OUT_DATE,IP.OUT_DEP_CODE,IP.COSTAMOUNT,IP.IN_DAYS,STD.AS_AVG_FEE,STD.AS_AVG_DAYS,STD.AS_WEIGHT, \n ");
		sql.append(" DIAG.PD0101 DIAG_CODE,DIAG.PD0100 DIAG_DESC,OPER.PO0101 OPER_CODE,OPER.PO0100 OPER_DESC,DEP.DEP_NAME,IP.COSTAMOUNT-STD.AS_AVG_FEE FEEGAP,IP.IN_DAYS-STD.AS_AVG_DAYS DAYGAP, \n ");
		sql.append(" CASE WHEN COSTAMOUNT BETWEEN 0.8*STD.AS_AVG_FEE AND 1.2*STD.AS_AVG_FEE THEN 1 \n ");
		sql.append(" WHEN (COSTAMOUNT > 0.5*STD.AS_AVG_FEE AND COSTAMOUNT <0.8*STD.AS_AVG_FEE) THEN 2 \n ");
		sql.append(" WHEN (COSTAMOUNT > 1.2*STD.AS_AVG_FEE AND COSTAMOUNT <1.5*STD.AS_AVG_FEE) THEN 3 \n ");
		sql.append(" WHEN COSTAMOUNT <=0.5*STD.AS_AVG_FEE THEN 4 \n ");
		sql.append(" WHEN COSTAMOUNT >= 1.5*STD.AS_AVG_FEE THEN 5 END CASE_FLAG \n ");
		sql.append(" FROM ( \n ");
		sql.append(" SELECT BASE.PMAININDEX,BASE.P00050000 MR_NO,BASE.P00060000 PNAME,BASE.P00090000 AGE_YEAR,BASE.P00090001 AGE_MONTH,BASE.P00090002 AGE_DAY, \n ");
		sql.append(" TO_CHAR(IP.P00290000,'YYYY-MM-DD') OUT_DATE,IP.P00300001 OUT_DEP_CODE,IP.P00800000 COSTAMOUNT,IP.P00320000 IN_DAYS FROM DRGS.MR_BASEINFO BASE ,DRGS.MR_IPINFO IP \n ");
		sql.append(" WHERE BASE.PMAININDEX = IP.PMAININDEX AND TO_CHAR(IP.P00290000,'YYYY-MM-DD')    \n ");
		sql.append(" BETWEEN :dateFrom AND :dateTo \n ");
		if(hospCode.contains(",")){
			sql.append(" AND INSTR(:hospCode,BASE.P00010001)>0 \n ");
		}else{
			sql.append(" AND BASE.P00010001=:hospCode \n ");
		}
		sql.append(" ) IP LEFT JOIN DRGS.T_PATIENTDRG PD ON PD.PARID = IP.PMAININDEX  AND PD.VER_REGION=:verRegion   \n ");
		sql.append(" LEFT JOIN DRGS.T_ANNUALSTD STD ON STD.AS_DRG_CODE = PD.PARDRG_CODE AND STD.AS_ID = :stdId \n ");
		sql.append(" LEFT JOIN DRGS.MR_DIAGNOSE DIAG ON DIAG.PMAININDEX = IP.PMAININDEX AND DIAG.PDTYPE = 'M'  \n ");
		sql.append(" LEFT JOIN DRGS.MR_OPERATION OPER ON OPER.PMAININDEX = IP.PMAININDEX AND OPER.POTYPE = 'M' and oper.po1400 = '1'  \n ");
		sql.append(" LEFT JOIN DRGS.T_DEPARTMENT DEP ON DEP.DEP_CODE = IP.OUT_DEP_CODE     \n ");
		sql.append(" WHERE PD.PARDRG_CODE = :drgCode \n ");
		if("".equals(code))
		{
			sql.append(" ORDER BY ABS(FEEGAP) DESC \n ");
		}
		if("G".equals(code))
		{
			sql.append(" ) GROUP BY ROLLUP(CASE_FLAG) ORDER BY CASE_FLAG \n ");
		}
		else if(code.matches("[1-5]"))
		{
			sql.append(" )WHERE CASE_FLAG = '"+code+"' GROUP BY OUT_DEP_CODE,DEP_NAME ORDER BY CNT DESC \n ");
		}
		else if("0".equals(code)){
			sql.append(" ) GROUP BY OUT_DEP_CODE,DEP_NAME ORDER BY CNT DESC \n ");
		}
		return jdbcTempSeverice.queryForList(sql.toString(),paramMap);
	}

}
